package top.lshaci.learning.springboot.web.fastjson.writer;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.codec.DateTimeCodec;
import com.alibaba.fastjson2.writer.ObjectWriter;

import java.lang.reflect.Type;
import java.time.LocalTime;
import java.util.Locale;

/**
 * ObjectWriterLocalTime
 *
 * @author liangxu
 */
public class ObjectWriterLocalTime extends DateTimeCodec implements ObjectWriter<LocalTime> {

    public static final ObjectWriterLocalTime INSTANCE = new ObjectWriterLocalTime(null, null);

    public ObjectWriterLocalTime(String format, Locale locale) {
        super(format, locale);
    }

    @Override
    public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        jsonWriter.writeLocalTime((LocalTime) object);
    }

    @Override
    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        if (object == null) {
            jsonWriter.writeNull();
            return;
        }

        LocalTime time = (LocalTime) object;
        jsonWriter.writeString(time.toString());
    }
}
